package com.wangjie.stackandqueue;

import java.util.Stack;

/**
 * 232. 用栈实现队列
 *
 * @author jieshao
 * @date 2022/5/6 15:12
 */
public class StackImpQueue {
    private Stack<Integer> s1;
    private Stack<Integer> s2;

    public StackImpQueue() {
        s1 = new Stack<>();
        s2 = new Stack<>();
    }

    /**
     * 添加元素到队尾
     *
     * @param x
     */
    public void push(int x) {
        s1.push(x);
    }

    /**
     * 删除队首的元素并返回
     *
     * @return
     */
    public int pop() {
        // 先调用 peek() 保证 s2 非空
        peek();
        return s2.pop();
    }

    /**
     * 返回队首元素
     *
     * @return
     */
    public int peek() {
        if (s2.isEmpty()) {
            while (!s1.isEmpty()) {
                s2.push(s1.pop());
            }
        }
        return s2.peek();
    }

    /**
     * 判断队列是否为空
     *
     * @return
     */
    public boolean empty() {
        return s1.isEmpty() && s2.isEmpty();
    }
}